package kosta.model.friends;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import kosta.model.member.MemberVO;
import kosta.model.memo.MemoDao;
import kosta.model.memo.MemoVO;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class FriendsServiceImpl implements FriendsService{
	@Resource(name="friendsDaoImpl")
	private FriendsDao friendsDao;
	@Resource(name="memoDaoImpl")
	private MemoDao memoDao;

	@Override
	public List<FriendsVO> getFriendsList(String myid) throws Exception {
		return friendsDao.getFriendsList(myid);
	}

	@Override
	public List<FriendsVO> newFriends() throws Exception {
		return friendsDao.newFriends();
	}

	@Override
	public MemberVO searchById(String search) throws Exception {
		return friendsDao.searchById(search);
	}

	@Override
	public String joinFriend(FriendsVO fvo) throws Exception {
		FriendsVO friendsVO = friendsDao.findByFriend(fvo);
		String str="";
		if(friendsVO==null){
			friendsDao.joinFriend(fvo);
			str="친구추가 완료";
		}else{
			str="이미 추가된 친구입니다";
		}
		return str;
	}

	@Override
	public void outFriends(String[] no) throws Exception {
		for(int i=0; i<no.length; i++){
			System.out.println(no + "-------------- outMemo"+no[i]);
			friendsDao.outFriends(no[i]);
			}	
	}
	
	@Transactional
	@Override
	public MemberVO giftPoint(MemberVO mvo, String[] id, int point) throws Exception {
		MemberVO fmvo;
		HashMap map = new HashMap();
		map.put("point", point);
		for(int i=0; i<id.length; i++){
			fmvo = friendsDao.findByMember(id[i]);
			map.put("mvo", mvo);
			friendsDao.downPoint(map);
			mvo = memoDao.idCheck(mvo.getId());
			map.put("fmvo", fmvo);
			friendsDao.upPoint(map);
			fmvo = memoDao.idCheck(fmvo.getId());
		}
		return mvo = memoDao.idCheck(mvo.getId());
	}

	@Override
	public void sendMemos(MemoVO memoVO) throws Exception {
		memoDao.sendMemo(memoVO);
	}
	
	
	
	
}
